package com.bsi.login.service;
/*
 * Author: liusongs
 * Date: 2019/10/18 10:35
 * Content:  登陆类，用户密码校验成功后像oauth2获取token令牌
 */

import com.alibaba.fastjson.JSONObject;
import com.bsi.login.oauth.entity.Oauth2ReqParamVo;
import com.bsi.login.oauth.entity.OauthVisitModeEnum;
import com.bsi.login.oauth.handler.IOauthRequestHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;

@Service
public class LoginService {

    @Autowired
    private IOauthRequestHandler iOauthRequestHandler;


    public Map login(String username, String password, String userId) {
        Oauth2ReqParamVo oauth2ReqVo = new Oauth2ReqParamVo.Oauth2ReqVoBuilder().tokenModeBuiler(username, password,userId, OauthVisitModeEnum.token_mode, null);
        Map token = iOauthRequestHandler.tokenHandler(OauthVisitModeEnum.token_mode, null, oauth2ReqVo, Map.class);
        return token;
    }


    public String refreshToken(String refresh_token) {
        Oauth2ReqParamVo oauth2ReqVo = new Oauth2ReqParamVo.Oauth2ReqVoBuilder().refreshTokenModeBuiler(OauthVisitModeEnum.refresh_mode, refresh_token, null);
        Map token = iOauthRequestHandler.tokenHandler(OauthVisitModeEnum.refresh_mode, null, oauth2ReqVo, Map.class);
        return JSONObject.toJSONString(token);
    }

    public String checkToken(String token) {
        Oauth2ReqParamVo oauth2ReqVo = new Oauth2ReqParamVo.Oauth2ReqVoBuilder().checkTokenBuilder(token, null);
        Map token2 = iOauthRequestHandler.tokenHandler(OauthVisitModeEnum.check_mode, null, oauth2ReqVo, Map.class);
        return JSONObject.toJSONString(token2);
    }

    public String exit(String token) {
        Oauth2ReqParamVo oauth2ReqVo = new Oauth2ReqParamVo.Oauth2ReqVoBuilder().deleteTokenBuilder(token, null);
        Map token2 = iOauthRequestHandler.tokenHandler(OauthVisitModeEnum.delete_mode, null, oauth2ReqVo, Map.class);
        return JSONObject.toJSONString(token2);
    }
}
